Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Image processing with Java 2D

Sophisticated image processing support comes to Java in the 2D API

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Image processing is the art and science of manipulating digital images. It stands with one foot firmly in mathematics and the other in aesthetics, and is a critical component of graphical computer systems. If you've ever bothered with creating your own images for Web pages, you will no doubt appreciate the importance of Photoshop's image manipulation capabilities for cleaning up scans and clearing up less-than-optimal images.

If you did any image processing work in JDK 1.0 or 1.1, you probably remember that it was a little obtuse. The old model of image data producers and consumers is unwieldy for image processing. Before JDK 1.2, image processing involved MemoryImageSources, PixelGrabbers, and other such arcana. Java 2D, however, provides a cleaner, easier to use model.

This month, we'll examine the algorithms behind several important image-processing operations (ops) and show you how they can be implemented using Java 2D. We'll also show you how these ops are used to affect image appearance.

Because image processing is a genuinely useful standalone application of Java 2D, we've built this month's example, ImageDicer, to be as reusable as possible for your own applications. This single example demonstrates all of the image-processing techniques we'll cover in this month's column.

Note that shortly before this article went to publication, Sun released the Java 1.2 Beta 4 development kit. Beta 4 seems to give better performance for our example image processing operations, but it also adds some new bugs involving bounds checking of ConvolveOps. These problems affect the edge detection and sharpening examples we use in our discussion.

We think these examples are valuable, so rather than omit them altogether, we compromised: to ensure it runs, the example code reflects the Beta 4 changes, but we've retained the figures from the 1.2 Beta 3 execution so you can see the operations working correctly.

Hopefully, Sun will address these bugs before the final Java 1.2 release.

Image processing isn't rocket science

Image processing doesn't have to be difficult. In fact, the fundamental concepts are really quite simple. An image, after all, is just a rectangle of colored pixels. Processing an image is simply a matter of calculating a new color for each pixel. The new color of each pixel can be based on the existing pixel color, the color of surrounding pixels, other parameters, or a combination of these elements.

The 2D API introduces a straightforward image processing model to help developers manipulate these image pixels. This model is based on the java.awt.image.BufferedImage class, and image processing operations like convolution and thresholding are represented by implementations of the java.awt.image.BufferedImageOp interface.

Java 2D's image processing model, based on BufferedImageOps

The implementation of these ops is relatively straightforward. Suppose, for example, that you already have the source image as a BufferedImage called source. Performing the operation illustrated in the figure above would take only a few lines of code:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (2)
Login
Forgot your account info?

RequestBy Suhani on March 25, 2009, 12:57 amHi I am trying to do an image procesing project in which i need to detect a ship from image.could you provide some help.i will be thankfull for that.Thank...

Reply | Read entire comment

Add color to ImageBy Anonymous on February 9, 2009, 1:15 amHi, I have created a gallery of items, where each item has a icon associated with it. Just like windows folder viewer. When user selects any of the item the icon...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Carol Gerten's Fine Art site is the source of the Sargent painting used as a sample image. This site has a comprehensive collection of fine art images, carefully organized and cleanly scanned. http://sunsite.unc.edu/cjackson/
  • Take a cheap art history tour of Florence with the Virtual Uffizi. It has descriptions and images for every painting in the Uffizi. http://www.arca.net/uffizi/
  • Clifford Watson has written a nice introduction to image processing for the University of Washington. It offers a light treatment of the mathematics of convolution and includes some good sample images. http://www.cs.washington.edu/research/metip/tutor/tutor.html
  • If you're curious about Fourier transforms, which seem to pop up all over the place when you peak under the covers of image processing algorithms, check out this tutorial hosted by the University of Strathclyde. http://www.spd.eee.strath.ac.uk/~interact/fourier/fourier.html
  • Download the complete source for this column. http://www.javaworld.com/jw-09-1998/media/jw-09-media.jar
  • If you would like to perform the example image manipulations using the same source image, the Lady is available directly from JavaWorld's site, filename figure02.jpg. http://www.javaworld.com/jw-09-1998/media/figure02.jpg
  • Be sure to read Bill Day's other Media Programming columnsmin JavaWorld. /javaworld/topicalindex/jw-ti-media.html
  • I've archived Media Programming resources on my Web site. This archive contains the up-to-date media.jar file with code fixes for all of the examples in the column. http://reality.sgi.com/bday/Work/index.html